package me.ereach.exercises;

import org.junit.Test;
import java.util.Scanner;

public class Exercises {
    // 温度汇率转换
    public static void main(String[] args) {
        while (true) {
            // 扫描控制台输入
            Scanner scanner = new Scanner(System.in);
            System.out.print("Input 23C 89F $100 ¥100: ");
            String instr = scanner.next();

            if (instr.toLowerCase().trim().equals("quit"))
                break;
            else if (instr.toLowerCase().trim().equals("pass"))
                continue;
            // 摄氏温度输入
            else if (instr.toLowerCase().endsWith("c")) {
                float c = Float.parseFloat(instr.substring(0, instr.length() - 1));
                float f = c * 1.8f + 32;
                System.out.println(f + "F");
            }
            // 华氏温度
            else if (instr.toLowerCase().endsWith("f")) {
                float f = Float.parseFloat(instr.substring(0, instr.length() - 1));
                float c = (f - 32) / 1.8f;
                System.out.println(c + "C");
            } else if (instr.startsWith("$")) {
                float usd = Float.parseFloat(instr.substring(1));
                float rmb = usd * 6.5f;
                System.out.println("¥" + rmb);
            } else if (instr.startsWith("¥")) {
                float rmb = Float.parseFloat(instr.substring(1));
                float usd = rmb / 6.5f;
                System.out.println("$" + usd);
            } else
                System.out.println("Input error!");
        }
    }

    // 九九乘法表
    @Test
    public void consolePrint99() {
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < i + 1; j++) {
                System.out.print(i + "*" + j + "=" + i * j + "\t");
            }
            System.out.println();
        }
    }

    // 水仙花数
    @Test
    public void getFlowers3() {
        for (int i = 100; i <= 999; i++) {
            int n1 = i / 10 / 10 % 10;
            int n2 = i / 10 % 10;
            int n3 = i % 10;

            if (i == (int)(Math.pow(n1, 3) + Math.pow(n2, 3) + Math.pow(n3, 3)))
                System.out.println(i);
        }
    }

    // 质数
    @Test
    public void getPrime() {
        for (int i = 2; i <= 100; i++)
            if (checkPrime(i))
                System.out.println(i);
    }

    public boolean checkPrime(int num) {
        boolean primeFlag = true;

        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                primeFlag = false;
                break;
            }
        }

        return primeFlag;
    }

    // 斐波那契
    @Test
    public void getFib() {
        System.out.println(fib(1, 2, 10));
    }

    public long fib(long a, long b, long n) {
        long total = 0;

        if (n <= 2)
            throw new IllegalArgumentException("数据项数必须大于2");

        for (int i = 3; i <= n; i++) {
            total = a + b;
            a = b;
            b = total;
        }

        return total;
    }

    // 圆周率
    @Test
    public void calculatePi() {
        System.out.println(getPi(100000));
    }

    public double getPi(long n) {
        int sn = 0;
        double pi = 0;

        for (long i = 1; i <= n; i += 2) {
            sn++;
            if (sn % 2 == 0)
                pi -= 1 / (double)i;
            else
                pi += 1 / (double)i;
        }

        return pi * 4;
    }

    // 分解质因数一
    @Test
    public void fact1() {
        System.out.println(getFact1(126));
    }

    public String getFact1(int n) {
        String str = n + "=";

        int i = 2;

        while (i <= n) {
            if (n % i == 0) {
                str += i + "*";
                n = n / i;
            }
            else {
                i++;
            }
        }

        return str.substring(0, str.length()-1);
    }
}
